import pymongo
from string import punctuation
from matplotlib import pyplot as plt
import pylab
# 解决中文显示问题
pylab.mpl.rcParams['font.sans-serif'] = ['SimHei']
pylab.mpl.rcParams['axes.unicode_minus'] = False

###
# 北京二手信息数据展示
###

# 打开数据库获得信息
client = pymongo.MongoClient()
ganjix = client["ganjix"]
sample = ganjix["sample"]

# 批量更新地址为None的数据
sample.update_many({"area": None}, {"$set": {"area": "不明"}})
for item in sample.find():
    if item["area"]:
        area = [i for i in item["area"] if i not in punctuation]
    else:
        area = "不明"
    sample.update({"_id": item["_id"]}, {"$set": {"area": area}})
# [print(item["area"]) for item in sample.find().limit(100)]
# 获得各个地区的集合
area_list = []
for i in sample.find():
    area_list.append(i["area"][0])
area_index = list(set(area_list))

# 发帖次数
x = range(len(area_index))
y = [area_list.count(item) for item in area_index]
# 画图显示
plt.bar(x, y)
plt.xticks(x, area_index, rotation=45)
plt.show()
